//
// Created by ningjian on 18-6-6.
//

#ifndef RTPPULLER_STREAMTOMP4_HPP
#define RTPPULLER_STREAMTOMP4_HPP
extern "C"
{
//编码
#include <libavcodec/avcodec.h>
//封装格式处理
#include <libavformat/avformat.h>
//像素处理
#include <libswscale/swscale.h>
#include <libswresample/swresample.h>
#include <libavutil/mathematics.h>
#include <libavutil/time.h>
#include <libavutil/timestamp.h>
};

#include <iostream>

class StreamToMP4 {
public:
    StreamToMP4(std::string path_prefix,
                long timespace = 60,
                AVCodecID input_v_codec_id = AV_CODEC_ID_H264,
                AVCodecID input_a_codec_id = AV_CODEC_ID_AAC);

    bool WriteVideoFrameData(int f_len, uint8_t *f_data);

    bool WriteAudioFrameData(int f_len, uint8_t *f_data);

    bool Init();

    void Finsh();

private:

    long timespace{60};

private:

    AVCodecID input_v_codec_id;
    AVCodecID input_a_codec_id;

    AVCodec *v_input_code;
    AVCodecContext *v_input_codec;
    AVCodecParserContext *v_input_parse;

    AVPacket v_packet;
    AVFrame *v_frame;


    bool is_init_v_input{false};
    bool is_init_a_input{false};

    std::string path_prefix;
    std::string v_tmp_name;
    long start_tmp_time{0};
    std::string a_tmp_name;

    FILE* p_file_v{NULL};
    FILE* p_file_a{NULL};

};


#endif //RTPPULLER_STREAMTOMP4_HPP
